<script>
export default {
  name: "KnowledgeManage",
  data() {
    return {
      tableData: [],
      pageSize: 10,
      pageNum: 1,
      total: 0,
      keywords: '',
      centerDialogVisible: false,
      form: {
        id: '',
        title: '',
        type: '',
        content: '',
        publisherNo: ''
      },
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      }
    };
  },
  methods: {
    loadPost() {
      this.$axios.post(this.$httpUrl + '/knowledge/listPage', {
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        param: { keywords: this.keywords }
      }).then(res=>res.data).then(res=>{
        if (res.code === 200) {
          this.tableData = res.data;
          this.total = res.total;
        }
      });
    },
    resetParam(){
      this.keywords = ''
      this.loadPost()
    },
    // 删除
    del(id){
      this.$axios.get(this.$httpUrl + `/knowledge/del?id=${id}`).then(res => {
        if (res.data.code === 200) {
          this.$message.success('删除成功！');
          this.loadPost();
        } else {
          this.$message.error('删除失败！');
        }
      }).catch(error => {
        console.error('删除请求失败:', error);
        this.$message.error('请求异常，请检查控制台');
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
  },
  beforeMount() {
    this.loadPost();
  }
};
</script>

<template>
  <div>
    <div style="border-bottom: 5px">
      <el-input v-model="keywords" placeholder="标题/作者..." suffix-icon="el-icon-search" style="width: 200px"
                @keyup.enter.native="loadPost"></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
    </div>
    <!-- 表格列调整为知识相关字段 -->
    <el-table :data="tableData" :header-cell-style="{background:'#d2bcad'}"
              border>
      <el-table-column prop="id" label="ID">
      </el-table-column>
      <el-table-column prop="title" label="标题">
      </el-table-column>
      <el-table-column prop="publisherNo" label="作者" >
      </el-table-column>
      <el-table-column prop="type" label="类型标签" >
      </el-table-column>
      <el-table-column prop="createTime" label="发布时间" >
      </el-table-column>
<!--      <el-table-column prop="likes" label="点赞数" >
      </el-table-column>
      <el-table-column prop="comments" label="评论数" >
      </el-table-column>-->
      <el-table-column prop="operator" label="操作" width="200">
        <template slot-scope="scope">
          <el-popconfirm
              confirm-button-text='好的'
              confirm-button-type="warning"
              cancel-button-text='不用了'
              icon="el-icon-info"
              icon-color="red"
              title="该操作不可逆,确定删除吗? "
              @confirm="del(scope.row.id)"
              style="margin-left: 5px">
            <el-button slot="reference" size="small" type="danger" >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="display: flex">
      <!-- 分页 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[10, 15, 20, 25]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next"
          :total="total"
      />
      <!-- 表单弹窗 -->
<!--      <el-form :model="form">
        <el-form-item label="分类" style="display: flex">
          <el-select v-model="form.type">
            <el-option label="小技巧" value="小技巧" />
            <el-option label="小知识" value="小知识" />
            <el-option label="小趣事" value="小趣事" />
            <el-option label="新手养宠" value="新手养宠" />
            <el-option label="饮食与营养" value="饮食与营养" />
            <el-option label="健康与医疗" value="健康与医疗" />
          </el-select>
        </el-form-item>
      </el-form>-->
    </div>
  </div>
</template>